<?php

/**

 * @copyright	Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.

 * @license		GNU General Public License version 2 or later; see LICENSE.txt

 */



defined('_JEXEC') or die;



jimport('joomla.application.component.modellist');



/**

 * Methods supporting a list of banner records.

 *

 * @package		Joomla.Administrator

 * @subpackage	com_bricrop

 * @since		1.6

 */

      

class RegionsModelRegions extends JModelList

{

	/**

	 * Constructor.

	 *

	 * @param	array	An optional associative array of configuration settings.

	 * @see		JController

	 * @since	1.6

	 */

	public function __construct($config = array())

	{

		if (empty($config['filter_fields'])) {

			$config['filter_fields'] = array(

				'id', 'a.id',

				'title', 'a.title',

				'published', 'a.published',

				'ordering', 'a.ordering',

				'created', 'a.created',

			);

		}

		parent::__construct($config);

	}



		

	/**

	 * Build an SQL query to load the list data.

	 *

	 * @return	JDatabaseQuery

	 * @since	1.6

	 */

	protected function getListQuery()
	{ 

		// Create a new query object.

		$db = JFactory::getDBO();

		$query = $db->getQuery(true);

		// Select some fields

		$query->select('a.*, a.parent as parent_id');

		// From the hello table

		$query->from('#__regions a ');

		$query->join('left','#__regions b ON a.id = b.parent ');

		// Filter by category.
		$country_id = $this->getState('filter.country_id');
		if (is_numeric($country_id)) {
			$query->where('a.parent >= '.(int) $country_id);
		}
		
		$orderCol	= $this->state->get('list.ordering', 'ordering');

		$orderDirn	= $this->state->get('list.direction', 'ASC');

		$query->order($db->escape($orderCol.' '.$orderDirn));

		return $query;		

	}



	/**

	 * Method to get a store id based on model configuration state.

	 *

	 * This is necessary because the model is used by the component and

	 * different modules that might need different sets of data or different

	 * ordering requirements.

	 *

	 * @param	string		$id	A prefix for the store id.

	 * @return	string		A store id.

	 * @since	1.6

	 */

	protected function getStoreId($id = '')

	{

		// Compile the store id.

		$id	.= ':'.$this->getState('filter.search');

		$id	.= ':'.$this->getState('filter.access');

		$id	.= ':'.$this->getState('filter.state');

		$id .= ':'.$this->getState('filter.language');



		return parent::getStoreId($id);

	}

	public function getCountryId()
	{
		return $this->getState('filter.country_id');	
	}

	/**

	 * Returns a reference to the a Table object, always creating it.

	 *

	 * @param	type	The table type to instantiate

	 * @param	string	A prefix for the table class name. Optional.

	 * @param	array	Configuration array for model. Optional.

	 * @return	JTable	A database object

	 * @since	1.6

	 */

	public function getTable($type = 'Region', $prefix = 'RegionsTable', $config = array())

	{

		return JTable::getInstance($type, $prefix, $config);

	}



	/**

	 * Method to auto-populate the model state.

	 *

	 * Note. Calling getState in this method will result in recursion.

	 *

	 * @since	1.6

	 */

	protected function populateState($ordering = null, $direction = null)

	{

		// Initialise variables.

		$app = JFactory::getApplication('administrator');

		// Load the filter state.

		$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');

		$this->setState('filter.search', $search);

		$country_id = $this->getUserStateFromRequest($this->context.'.filter.country_id', 'filter_country_id', '');
		$this->setState('filter.country_id', $country_id);

		$state = $this->getUserStateFromRequest($this->context.'.filter.state', 'filter_state', '', 'string');

		$this->setState('filter.state', $state);



		// Load the parameters.

		$params = JComponentHelper::getParams('com_regions');

		$this->setState('params', $params);

		// List state information.
		parent::populateState('a.title', 'asc');

	}

	

	

}

